<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let  status_config = _config('status_alias_map.code.status');
    if (is_del){
      if(in_right_admin('Code/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Code/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      // if(in_right_admin('Code/add')) btns['添加'] = function(){ iframe('code/edit') }
      if(in_right_admin('Code/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('Code/recycle')) btns['回收站'] = function(){ iframe('code/index?is_del=1') }
    }
    let forms_config = {
      code_group_id: {label:'兑换码组',name:'where[code_group_id][eq]',type:'select_one',config:[],value:this.$get('code_group_id','')},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden'},
      code_group_id: {name:'code_group_id', label:'组标题',type:'select_one',config:[], width:'120px'},
      code: {name:'code', label:'code'},
      password: {name:'password', label:'密码', width:'80px'},
      nickname: {name:'nickname', label:'使用用户', width:'180px'},
      use_time: {name:'use_time', label:'使用时间', width:'165px'},
      status: {name:'status', label:'状态',type:'radio_switch',can_edit:true,config:status_config,width:'80px',onchange:this.edit},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    if (!_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }

    return {
      config: {
        api: '/admin/code/'+(is_del?'recycle':'list'),
        title: "兑换码列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('Code/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('code/edit?'+s94Mjs.param_encode(param))
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('CodeGroup/edit')) return;
      ajax(`/admin/code/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (col.type=='radio_switch' && res.data==0) app.$refs['list_url'].list();
        if (col.type!='radio_switch' && res.data>0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/code_group/list',cache:true}, function (res){
      let map = decode_config(res.data.list, {content_key:'title', value_key:'id'});
      app.config.forms_config.code_group_id.config = map;
      app.config.list_cols.code_group_id.config = map;
      return false;
    })
  }
}
</script>
